package com.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.order.service.SentinelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @version 1.0
 * @author: yu.tan
 * @date: 2020/11/3 21:00
 * @Description:
 */
@Slf4j
@Service
public class SentinelServiceImpl implements SentinelService {

    //定义资源 value指定资源名称
    @SentinelResource("message")
    @Override
    public String getMessage() {
        return "message";
    }

    /**
     * 定义一个资源
     * blockHandler 定义资源内部发生BlockException时触发（捕获的时Sentinel定义的异常）
     * fallback 定义资源内部发生Throwable时触发
     *
     * @return
     */
    @SentinelResource(value = "message_a", blockHandler = "blockHandler", fallback = "fallback")
    @Override
    public String getMessageA() {
        int a=3/0;
        return "message_a";
    }

    /**
     * blockHandler
     *
     * @param e
     * @return
     */
    public String blockHandler(BlockException e) {
        log.error("触发：BlockException：{}", e);
        return "BlockException";
    }

    /**
     * fallback
     *
     * @param e
     * @return
     */
    public String fallback(Throwable e) {
        log.error("触发：Throwable：{}", e);
        return "Throwable";
    }
}
